package com.jcp.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jcp.entity.Attences;
import com.jcp.entity.OverTime;
import com.jcp.service.AttencesService;
import com.jcp.serviceimp.AttencesServiceImp;
import com.jcp.util.FaceIDUtil;

import org.json.JSONException;
import org.json.JSONObject;

/*
 * 与人脸识别对应的Servlet
 * 作用：处理人脸打卡、人脸登陆、人脸注册的请求和数据
 * author：戴布斯
 */
@WebServlet("/FaceIdServlet")
public class FaceIdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public FaceIdServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String op = "";
		// 获取页面传输的op值
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if (op.equals("login")) {
			// 得到人脸登录的ID
			int faceid=faceLogin(response, request);
			// 然后跳转到登录的Servlet
			System.out.println("faceid"+faceid);
		
		} else if (op.equals("clock")) {
			// 得到人脸登录的ID
			faceClock(response, request);
		} else if (op.equals("add")) {
			System.out.println("开始执行add");
			// 得到人脸登录的ID
			faceAdd(response, request);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	// 上传面部
	public void faceAdd(HttpServletResponse response, HttpServletRequest request) throws IOException {
		System.out.println("进入add");
		int userId = Integer.parseInt(request.getParameter("userId"));
		String base = request.getParameter("base");
		System.out.println("用户ID是：" + userId + ",base：" + base);
		boolean flag = FaceIDUtil.addFace(userId, base);
		System.out.println("用户ID：" + userId + "上传" + flag);

		PrintWriter out = response.getWriter();
		Map<String, String> map = new HashMap<String, String>();
		map.put("flag", flag + "");
		System.out.println("map:" + map);
		JSONObject json = new JSONObject(map);
		System.out.println("json:" + json);

		out.print(json);
		out.close();
	}

	// 面部登录
	public int faceLogin(HttpServletResponse response, HttpServletRequest request) throws IOException {
		String base = request.getParameter("base");
		System.out.println(base);
		int faceId = 0;
		faceId = FaceIDUtil.searchFace(base);// 返回识别成功的用户ID
		PrintWriter out = response.getWriter();
		Map<String, String> map = new HashMap<String, String>();
		map.put("faceId", faceId + "");
		System.out.println("map:" + map);
		JSONObject json = new JSONObject(map);
		System.out.println("json:" + json);
		if (faceId != 0) {
			System.out.println("识别成功，用户ID是：" + faceId);
		} else {
			System.out.println("识别失败");
		}
		System.out.println("输出试试");
		out.print(json);
		out.close();
		return faceId;
	}

	// 面部打卡
	public void faceClock(HttpServletResponse response, HttpServletRequest request) throws IOException {
		String base = request.getParameter("base");
		System.out.println(base);
		int faceId = 0;
		faceId = FaceIDUtil.searchFace(base);// 返回识别成功的用户ID
		PrintWriter out = response.getWriter();
		Map<String, String> map = new HashMap<String, String>();
		if (faceId != 0) {
			System.out.println("识别成功，用户ID是：" + faceId);
			boolean flag = insertClock(faceId);
			if (flag) {
				map.put("faceId", faceId + "");
				System.out.println("插入记录成功");
			} else {
				map.put("faceId", "0");
				System.out.println("插入记录失败");
			}
		} else {
			map.put("faceId", "0");
			System.out.println("识别失败");
		}
		System.out.println("map:" + map);
		JSONObject json = new JSONObject(map);
		System.out.println("json:" + json);
		out.print(json);
		out.close();
	}

	// 插入记录打卡到数据库
	public boolean insertClock(int userId) {
		// 判断上班、下班，迟到了没有
		/*
		 * yyyy表示年，MM表示月，dd表示天，hh表示用12小时制， HH表示用24小时制，mm表示分，ss表示秒，SSS表示毫米
		 */
		boolean flag = false;
		int attFlag = 0;
		double overTime = 0;
		// 获得当前时间小时和分钟
		Date date = new Date();
		SimpleDateFormat h = new SimpleDateFormat("HH");
		SimpleDateFormat m = new SimpleDateFormat("mm");
		String hh = h.format(date);
		String mm = m.format(date);
		System.out.println("现在是" + hh + ":" + mm);

		AttencesService attencesService = new AttencesServiceImp();
		int clockNum = attencesService.clockNum(userId);
		System.out.println("用户" + userId + "今日已经打卡了" + clockNum + "次");
		if (clockNum == 0) {// 上班打卡
			if ((Integer.parseInt(hh) >= 0 && Integer.parseInt(hh) < 8)
					|| (Integer.parseInt(hh) == 8 && Integer.parseInt(mm) == 00)) {
				attFlag = 0;
				overTime = 0;
				System.out.println("这是上班打卡，现在还没迟到");
			} else {
				attFlag = 1;
				overTime = 0;
				System.out.println("这是上班打卡，现在迟到了");
				System.out.println("状态标记：" + attFlag + "，加班时长：" + overTime);
			}
			Attences attences = new Attences(userId, attFlag, overTime);
			System.out.println("要插入的对象：" + attences);
			flag = attencesService.insertClock(attences);
		} else if (clockNum == 1) {// 下班打卡
			if ((Integer.parseInt(hh) > 8 && Integer.parseInt(hh) < 17)
					|| (Integer.parseInt(hh) == 17 && Integer.parseInt(mm) == 00)) {
				attFlag = 2;
				overTime = 0;
				System.out.println("这是下班打卡，现在早退了");
			} else if ((Integer.parseInt(hh) > 17 && Integer.parseInt(hh) < 24)
					|| (Integer.parseInt(hh) == 24 && Integer.parseInt(mm) == 00)) {
				attFlag = 3;
				if (Integer.parseInt(hh) == 17 && Integer.parseInt(mm) > 0) {
					overTime = Integer.parseInt(mm);
				} else if (Integer.parseInt(hh) > 17) {
					overTime = (Integer.parseInt(hh) - 17) + (Integer.parseInt(mm) / 60);
				}
				System.out.println("这是下班打卡，加班了" + overTime + "小时");
			}
			Attences attences = new Attences(userId, attFlag, overTime);
			System.out.println("要插入的对象：" + attences);
			flag = attencesService.insertClock(attences);
		} else {
			System.out.println("重复打卡");
		}
		System.out.println("打卡" + flag);
		return flag;
	}

}
